home *** CD-ROM | disk | FTP | other *** search
- #!/usr/bin/perl
- #
- # mhpped - process backticks in comp, repl, and forw components
- #
- # Taken from "testedit" by Jerry Peek <jerry@ora.com>;
- # abuser: Jerry Sweet <jsweet@irvine.com>
- #
- # $Header: /usr/t/users/jsweet/src/mh-front/RCS/mhpped,v 1.5 1992/11/12 10:03:47 jsweet Exp $
-
- #
- # Subprograms
- #
-
- sub bail {
- unlink $temp;
- exit 1;
- }
-
- sub gripe {
- print STDERR "$prog: ", @_, "\n";
- }
-
- sub barf {
- &gripe(@_);
- &bail;
- }
-
- sub check {
- local($status) = @_;
- &barf("problem writing to temporary draft file \"$temp\"- $!")
- unless $status;
- }
-
- sub sh {
- local($_) = @_;
- local($st);
-
- if ($verbose) {
- print "\t$_\n";
- }
-
- return if $debug;
-
- $st = system "$_";
-
- if ($st >> 8) {
- split;
- &barf("$_[0] failed with status ", $st >> 8);
- exit 1;
- }
- }
-
- sub read_profile {
- # Defines associative array %PROFILE_COMPONENT indexed by lower case
- # component name. Continuation lines are tacked on. New
- # occurrences of a component wipe out previous occurrences of the
- # same component, but probably shouldn't.
-
- local($profile) = defined $ENV{'MH'} ? $ENV{'MH'} :
- $ENV{'HOME'} . "/.mh_profile";
- local($profile_component) = 'x-bogus';
- local($_);
-
- &barf("can't open profile \"$profile\" - $!")
- unless open(PROFILE, "<$profile");
-
- while (<PROFILE>) {
- /^(\S+):\s*/ && do {
- ($profile_component = $1) =~ tr/A-Z/a-z/;
- $PROFILE_COMPONENT{$profile_component} = $';
- };
-
- /^[ \t]/ && do {
- $PROFILE_COMPONENT{$profile_component} .= $_;
- };
- }
- }
-
- #
- # Main
- #
-
- umask 077; # This should probably be adjustable, but isn't at the moment.
-
- ($prog = $0) =~ s|.*/||; # Get the invocation name of this program
-
- # Check the profile
-
- &read_profile;
- if (defined $PROFILE_COMPONENT{$prog}) {
- push(@Args, split(' ', $PROFILE_COMPONENT{$prog}));
- }
-
- # Check the command line arguments
-
- push(@Args, @ARGV);
-
- while ($_ = shift @Args) {
- /^-debug$/ && do { ++$debug; next; };
-
- /^-editor$|^-edit$/ && do { $editor = shift @Args; next; };
-
- /^-entire$/ && do { ++$entire; next; };
-
- /^-help$/ && do {
- print <<"xxEndHelpxx";
- $prog - pre-process an MH composition template
- Usage: $prog [-(no)debug] [-editor editor] [-noeditor] [-(no)entire] [-help]
- [-(no)verbose] [file]
- Options:
- -debug print the message draft and invoke default whatnow
- -editor editor invoke the specified editor following processing
- -entire pre-process the entire message, including the body
- -help print this message
- -nodebug negates the -debug option
- -noeditor disables automatic invocation of an editor
- -noentire negates the -entire option; only the header is processed
- -noverbose negates the -verbose option
- -verbose print what's going on as it's being done
- file if omitted, envar \$mhdraft names the draft file
- xxEndHelpxx
-
- exit 1;
- };
-
- /^-nodebug$/ && do { --$debug; next; };
- /^-noeditor$|^-noedit$/ && do { $editor = ''; next; };
- /^-noentire$/ && do { --$entire; next; };
- /^-noverbose$/ && do { --$verbose; next; };
-
- /^-verbose$/ && do { ++$verbose; next; };
-
- /^[^\-]/ && do { $file = $_; next; };
-
- &barf("unrecognized command line option \"$_\"");
- }
-
- $debug = $debug > 0;
- $entire = $entire > 0;
- $verbose = $verbose > 0;
-
- # Do some initial setup.
-
- $temp = "/tmp/$prog.$$";
- $SIG{'HUP'} = $SIG{'INT'} = $SIG{'TERM'} = 'bail';
-
- # Figure out the name of the draft file
-
- if (! $file) {
- if (defined $ENV{'mhdraft'}) {
- $file = $ENV{'mhdraft'};
- }
- else {
- &barf("no draft file specified");
- }
- }
- else {
- $ENV{'mhdraft'} = $file;
- }
-
- # Let's start running our dirty fingers over the draft file...
-
- &barf("can't open draft file \"$file\" - $!")
- unless open(DRAFT, "<$file");
- &barf("can't create temporary draft file \"$temp\" - $!")
- unless open(TEMP, ">$temp");
-
- while (<DRAFT>) {
- last if (/^$|^-/ && ! $entire);
-
- if (/\`(.+)\`/) {
- $torun = $1;
- $prefix = $`;
- $suffix = $';
-
- print("\t$torun\n") if ($verbose || $debug);
- chop($result = `$torun`);
- if ($? >> 8) {
- &gripe("$file: line $.: command \"$torun\" returned error status ",
- $? >> 8);
- }
- next if ($prefix =~ /^\s*$|^\S+:\s*$/) &&
- ($suffix =~ /^\s*$/) &&
- ! $result;
- # If there was no result, and there was nothing important
- # before or after the backtick construct, then don't even
- # bother printing a line. This allows us to omit
- # header components programmatically.
-
- &check(print(TEMP $prefix, $result, $suffix));
- }
- else {
- &check(print TEMP $_);
- }
- }
-
- if (! $entire) {
- &check(print TEMP "-------\n");
- grep(&check(print TEMP $_) && 0, <DRAFT>);
- }
-
- &barf("problem closing temporary draft file \"$temp\" - $!")
- unless close(TEMP);
-
- close(DRAFT);
-
- &sh("cp $temp $file");
- system('/bin/cat', $temp) if $debug;
-
- print("\trm $temp\n") if ($verbose || $debug);
- unlink $temp;
-
- &sh("$editor $file") if $editor;
-